/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;

import Atxy2k.CustomTextField.RestrictedTextField;
import Controlador.Bean.Produccion.Receta;
import gui.General.Main;
import java.util.ArrayList;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import lib.Validaciones;


public class agregarProducto extends javax.swing.JInternalFrame {

    /**
     * Creates new form agregarProducto
     */
    public ArrayList<String> seleccionfases;
    public ArrayList<String> vseleccionfases;
    private String idBuscar;
    private String nombreBuscar;
    AjusteReceta ar;
    
    public agregarProducto(String idp, String nomp) {
//        this.ar = ar;
        initComponents();
        this.idBuscar = idp;
        this.nombreBuscar = nomp;
        initComp2();
//        setTitle("Agregar Producto");
        
    }
    
    
    private void initComp2() {
//         setTb2(new TableModel2());
//         tblIngredientes.setModel(getTb2());
//         tblIngredientes.setRowSelectionAllowed(true);
        txtIdProducto.setText(idBuscar);
        txtIdProducto.setEnabled(false);
        txtNombreProducto.setText(nombreBuscar);
        txtNombreProducto.setEnabled(false);
        txtIdReceta.setEnabled(false);
        RestrictedTextField restricted = new RestrictedTextField(txtCantidadProducto);
        restricted.setLimit(5);
//         restricted.setOnlyNums(true);
//         tblProductos.setModel(tb);
//         tblProductos.setRowSelectionAllowed(true);
//         txtIdProducto.setEditable(false);
//         tipos = Controlador.Service.Almacen.GestionAlmacenes.obtenerTipo();
//         cmbTipo.removeAllItems();
//         cmbUnidadMedida.removeAllItems();
//         cmbUnidadMedida.addItem("Seleccionar");
//         cmbUnidadMedida.addItem("Kg.");
//         cmbUnidadMedida.addItem("m.");
//         cmbUnidadMedida.addItem("gr.");
//         cmbUnidadMedida.addItem("Unidades");
//         cmbTipo.addItem("Seleccionar");
//         for(int i=0; i<tipos.size(); i++){
//             cmbTipo.addItem(tipos.get(i).getNombre());
//         }
//         tblProductos.setDefaultRenderer(JButton.class, new MiRender());
//         tblProductos.setDefaultEditor( JButton.class, new MiEditor());
//         btnBuscar.setMnemonic(KeyEvent.VK_F2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtIdProducto = new javax.swing.JTextField();
        txtNombreProducto = new javax.swing.JTextField();
        txtCantidadProducto = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtIdReceta = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        checkBoxHorneado = new javax.swing.JCheckBox();
        checkBoxEmpaquetado = new javax.swing.JCheckBox();
        checkBoxRellenado = new javax.swing.JCheckBox();
        checkBoxDecorado = new javax.swing.JCheckBox();
        btnContinuar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Asignar Fases Producto Final");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        jLabel3.setText("Descripción:");

        txtDescripcion.setColumns(20);
        txtDescripcion.setRows(5);
        txtDescripcion.setMinimumSize(new java.awt.Dimension(6, 20));
        jScrollPane1.setViewportView(txtDescripcion);

        jLabel2.setText("Cantidad por Paquete:");

        jLabel1.setText("Nombre de Producto:");

        jLabel4.setText("IdProducto:");

        txtIdProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdProductoActionPerformed(evt);
            }
        });

        txtNombreProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreProductoActionPerformed(evt);
            }
        });

        jLabel5.setText("IdReceta:");

        txtIdReceta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdRecetaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtIdReceta)
                    .addComponent(txtNombreProducto)
                    .addComponent(txtIdProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCantidadProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(51, 51, 51))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtIdProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtNombreProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtIdReceta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(txtCantidadProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)))))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Procesos"));

        checkBoxHorneado.setSelected(true);
        checkBoxHorneado.setText("Masa / Horneado");
        checkBoxHorneado.setEnabled(false);
        checkBoxHorneado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxHorneadoActionPerformed(evt);
            }
        });

        checkBoxEmpaquetado.setSelected(true);
        checkBoxEmpaquetado.setText("Empaquetado");
        checkBoxEmpaquetado.setEnabled(false);

        checkBoxRellenado.setText("Rellenado");

        checkBoxDecorado.setText("Decorado");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(checkBoxHorneado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
                .addComponent(checkBoxRellenado)
                .addGap(126, 126, 126)
                .addComponent(checkBoxDecorado)
                .addGap(122, 122, 122)
                .addComponent(checkBoxEmpaquetado)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkBoxHorneado)
                    .addComponent(checkBoxRellenado)
                    .addComponent(checkBoxDecorado)
                    .addComponent(checkBoxEmpaquetado))
                .addGap(138, 138, 138))
        );

        btnContinuar.setText("Continuar");
        btnContinuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnContinuarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnContinuar)
                .addGap(212, 212, 212)
                .addComponent(jButton2)
                .addGap(206, 206, 206))
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(btnContinuar))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public int validar(){
        int error = 0;
        boolean vali = Validaciones.validarEntero(txtCantidadProducto.getText());
        if(txtCantidadProducto.getText().isEmpty()) {
            error=2;
        }
        else{
           if(!vali){
        error=1;}
        }
//        int indice = (int)cmbTipo.getSelectedIndex();
//        System.out.println("indice...."+indice);
//        System.out.println("fecha...."+jdpFecha.getDate());
//        if(txtNombre.getText().isEmpty()){
//        error=1;}
//        else if((jdpFecha.getDate()== null) || (jdpFecha.getDate().after(today))){
//        error=2; }
//        else if((indice==0))
//        { error=3;  }
//        else if(txtDescripcion.getText().isEmpty()){
////        error=4;    
//        }
        
        
        
        return error;
        
        
    }
    private void btnContinuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnContinuarActionPerformed
        try {
        seleccionfases = new ArrayList();
        if(checkBoxHorneado.isSelected())
            seleccionfases.add(checkBoxHorneado.getText());
        if(checkBoxRellenado.isSelected())
            seleccionfases.add(checkBoxRellenado.getText());
        if(checkBoxDecorado.isSelected())
            seleccionfases.add(checkBoxDecorado.getText());
        if(checkBoxEmpaquetado.isSelected())
            seleccionfases.add(checkBoxEmpaquetado.getText());
        vseleccionfases = seleccionfases;
        int error = validar();  
           
           if(error==1){
           JOptionPane.showMessageDialog(this,"Cantidad debe ser entero", "Advertencia", JOptionPane.OK_OPTION);
               
           }
           else
           if(error==2){
               JOptionPane.showMessageDialog(this,"Ingrese la cantidad", "Error en ingreso de Datos", JOptionPane.OK_OPTION);
           }
            else
               if(vseleccionfases.isEmpty()){
               JOptionPane.showMessageDialog(this,"Selecciona mínimo un proceso", "Error en ingreso de Datos", JOptionPane.OK_OPTION);
           } 
//           else
//            if(error==4){
//               JOptionPane.showMessageDialog(this,"Ingrese una descripcion");
//           }  
           else
            if(error==0){
           //         System.out.println("dato.."+cmbTipo.getSelectedIndex());
       //System.out.println("dato.."+cmbTipo.getSelectedIndex()-1);   
       
        
//        agregar receta
//        Double.parseDouble(txtCantidadProducto.getText())
        
        Receta re = new Receta(txtIdReceta.getText(), idBuscar, nombreBuscar, Double.parseDouble(txtCantidadProducto.getText()), " ", txtDescripcion.getText(), "1");
//        Receta re = new Receta(" ", idBuscar, nombreBuscar, 43.4, " ", "hola mundo", "1");
      
        if(txtIdReceta.getText().isEmpty()){
           
          try {
            
            Controlador.Service.Produccion.GestionProduccion.agregarReceta(re);
            System.out.println("efectua la insercion");
//            JOptionPane.showMessageDialog(this,"Se ingreso datos");
            //        para continuar siguiente ventana
        AjusteReceta arp = new AjusteReceta( seleccionfases,re);
        Main.getDesktop().add(arp);
//        ar.actualizar(seleccionfases);
        
        arp.setVisible(true);
        this.dispose();
            
        } catch (Exception ex) { 
            System.out.println("nno guarda");
        }
                                          
        }
//        else{
////            Controlador.Service.Produccion.GestionProduccion.actualizarMaquinas(nm);
////            JOptionPane.showMessageDialog(this,"Se actualizo datos");
//        }
        
        
    
        
        

        
                
//
//        fecha = jdpFecha.getDate();
//        Maquina nm = new Maquina(txtIdMaquina.getText(), txtNombre.getText(),  tipos.get(cmbTipo.getSelectedIndex()-1).getIdTipoMaquina(),  txtDescripcion.getText() , fecha, "1", "1" );
//        
//           
//         tb.actualizarTabla();
//        txtIdMaquina.setText("");
//        txtNombre.setText("");
//        txtDescripcion.setText("");
//        cmbTipo.setSelectedIndex(0);
//        cmbTipo.setEnabled(true);
            }     
            
      
       
//        dpFecha.setDate(today);
         
        } catch (Exception ex) {
        System.out.println("errror"+ex.getMessage());
        System.out.println("errror"+ex);
        }
        
        
        
        
        
        
        
        
//        detalleProducto dp = new detalleProducto();
//        Main.getDesktop().add(dp);
//        dp.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnContinuarActionPerformed

    private void txtIdProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdProductoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdProductoActionPerformed

    private void txtNombreProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreProductoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreProductoActionPerformed

    private void txtIdRecetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdRecetaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdRecetaActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void checkBoxHorneadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxHorneadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_checkBoxHorneadoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnContinuar;
    private javax.swing.JCheckBox checkBoxDecorado;
    private javax.swing.JCheckBox checkBoxEmpaquetado;
    private javax.swing.JCheckBox checkBoxHorneado;
    private javax.swing.JCheckBox checkBoxRellenado;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtCantidadProducto;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtIdProducto;
    private javax.swing.JTextField txtIdReceta;
    private javax.swing.JTextField txtNombreProducto;
    // End of variables declaration//GEN-END:variables
}
